<?php
$allRoles = $this->allRole;
?>

<div class="ad_title_ctn">
    <h2>Quản lý người dùng</h2>
</div>

<div class="ad_content_ctn">
    <div class="user_critetia_ctn">
        <div class="left" style="width:300px">
            <span>Role:</span>
            <select id="cboRole">
                <option value="ALL">All</option>
                <?php

                foreach ($allRoles as $aRole) {
                    echo '  <option value="'.$aRole->id.'">'.$aRole->name.'</option>';
                }

                ?>
            </select>
        </div>

        <div class="left">
            <span>Tên đăng nhập: </span>
            <input class="k-textbox" style="width: 300px" id="txtName" >
        </div>
        <div class="clear"></div>

    </div>
    <div class="user_search_ctr">
        <a id="btnSearch"  class="k-button k-button-icontext">Tìm kiếm</a>
    </div>
    <div id="user_grid"></div>
</div>
<script type="text/x-kendo-template" id="template">
    <div class="toolbar">
        <a class="k-button k-button-icontext  k-grid-edit"
           href="/admin/user/add">
            <span class="k-icon k-add"></span>Tạo mới
        </a>
    </div>
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".user_critetia_ctn").keyup( function(event) {
            if ( event.which == 13 ) {
                event.preventDefault();
                doSearch();
            }
        });

        $("#cboRole").kendoComboBox({
            filter: "contains",
            suggest: true,
            index: 3
        });

        var cboRole = $("#cboRole").data("kendoComboBox");

        dataSource = new kendo.data.DataSource({
            type: "jsonp",
            transport: {

                read: function(options) {
                    options.data.name=$('#txtName').val();
                    options.data.role=cboRole.value();

                    $.ajax( {
                        url: "<?php echo '/admin/user/list'  ?>",
                        data: options.data, 
                        success: function(result) {
                            options.success(result);
                        },
                        dataType: "json",
                        type: "POST"
                    });
                },

                destroy: function(options) {
                    $.ajax( {
                        url: "/admin/user/remove?id="+options.data.id,
                        data: options.data,
                        success: function(result) {
                            if(result.success){
                                options.success(options.data.models);
                                $('#error_ctn').addClass('blue_text').html('Xóa thành công.');
                            }else{
                                $('#error_ctn').removeClass('blue_text').html(result.msg);
                                grid = $("#user_grid").data("kendoGrid");
                                // cancel changes
                                grid.cancelChanges();
                            }
                        },
                        dataType: "json",
                        type: "POST"
                    });
                }
            },
            schema: {
                model: {
                    id: "id",
                    fields: {
                        id: {editable: false,  type: "number" },
                        name: { type: "string" },
                        username: { type: "string" },
                        email: { type: "string" },
                        phone_number: { type: "string" },
                        active: { type: "boolean" },
                        deletable: { type: "boolean" },
                        created_date: { type: "date" }
                    }
                },
                data: "rows",
                total: "total"
            },
//                    batch: true,
            pageSize: 30,
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        });

        $("#user_grid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable: true,

            columnMenu: true,
            sortable: true,
            resizable:true,
            toolbar:kendo.template($("#template").html()),

            columns: [
                { field: "name", title: "Tên", width: 100, template:" <a href='<?php echo '/admin/user/edit?id='  ?>#= id #'>#= name #</a> " },
                { field: "username", title: "Tên đăng nhập", width: 120 },
                { field: "email", title: "Email", width: 120 },
                { field: "phone_number", title: "Phone", width: 120 },
                { field: "active", title: "Active", width: 40 },
                { field: "created_date", title: "Ngày tạo", width: 80,
                    template: '# if(created_date != null){#' +
                            '#= kendo.toString(created_date,"dd/MM/yyyy") #' +
                            '#}#'},
                { command:  [{name: "destroy", text: ""}],  title: "&nbsp;", width: 40 }

            ],
            editable: "inline"
        });

        function doSearch(){
            grid = $("#user_grid").data("kendoGrid");
            grid.dataSource.page(1);
            grid.dataSource.read();
        }

        $('#btnSearch').click(function(){
            doSearch();
        })
    });

</script>